﻿using System;
using System.Collections.Generic;
using System.Linq;
using Enums;
using XF.ExControls;
using XF.Common;
using System.Data;
using System.Windows.Forms;

namespace KYBM
{
    public partial class FrmCustomerCard : XFFormEx
    {
        private readonly XF.BLL.Base_Customer bll = new XF.BLL.Base_Customer();
        public XF.Model.Base_Customer Customer { get; set; } = new XF.Model.Base_Customer();
        public FrmCustomerCard()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 客户数据校验
        /// </summary>
        /// <returns>校验结果信息，为空表示校验通过</returns>
        private string CheckInput()
        {
            XF.Model.Base_Customer model;
            if (tbCode.Text.Trim().Equals(string.Empty))
            {
                return MessageText.CHECK_ERROR_STO_CUSTOMER_CODE;
            }
            if (tbName.Text.Trim().Equals(string.Empty))
            {
                return MessageText.CHECK_ERROR_STO_CUSTOMER_NAME;
            }
            model = bll.GetModelByName(tbName.Text);
            if (model != null && model.Id != Customer.Id)
            {
                return MessageText.CHECK_REPEAT_CUSTOMER_NAME;
            }
            if (!tbNo.Text.Equals(string.Empty))
            {
                if (zDataConverter.ToInt(tbNo.Text, -999) == -999)
                {
                    return MessageText.CHECK_ERROR_DTO_CUSTOMER_NO;
                }
                int payType = zDataConverter.ToInt(cmbPayType.SelectedValue);
                int no = zDataConverter.ToInt(tbNo.Text, -999);
                model = bll.GetModel(payType, no);
                if (model != null && model.Id != Customer.Id)
                {
                    return MessageText.CHECK_REPEAT_CUSTOMER_NO;
                }
            }
            return string.Empty;
        }

        private void FrmCustomerCard_Load(object sender, EventArgs e)
        {
            cmbPayType.DisplayMember = MessageText.LISTITEM_TEXT;
            cmbPayType.ValueMember = MessageText.LISTITEM_VALUE;
            cmbPayType.DataSource = GenerateData.GetPayType(false); 

            cmbPayType.SelectedValue = Customer.PayType;

            if (Customer.Id >= 0)
            {
                tbName.Text = Customer.CustomerName;
                tbCode.Text = Customer.CustomerCode;
                tbContacts.Text = Customer.Contacts;
                tbPhone.Text = Customer.Phone;
                tbNo.Text = Customer.No.ToString();
                tbAccount.Text = Customer.Account;
                tbAddress.Text = Customer.Address;
                tbCustomerRemark.Text = Customer.Remark;
            }
        }

        private void tsBtnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tsBtnSure_Click(object sender, EventArgs e)
        {
            string msg = CheckInput();
            if (msg.Equals(string.Empty))
            {
                Customer.CustomerName = tbName.Text;
                Customer.CustomerCode = tbCode.Text;
                Customer.Contacts = tbContacts.Text;
                Customer.PayType = zDataConverter.ToInt(cmbPayType.SelectedValue);
                Customer.Phone = tbPhone.Text;
                if (tbNo.Text.Equals(string.Empty))
                {
                    Customer.No = null;
                }
                else
                {
                    Customer.No = zDataConverter.ToInt(tbNo.Text);
                }
                Customer.Account = tbAccount.Text;
                Customer.Address = tbAddress.Text;
                Customer.Remark = tbCustomerRemark.Text;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                QQMessageBox.Show(
                this,
                msg,
                MessageText.MESSAGEBOX_CAPTION_TIP,
                QQMessageBoxIcon.Information,
                QQMessageBoxButtons.OK);
            }
        }
    }
}
